package com.compass.mobile.ui.dialog

import UserInfoManager
import android.view.Gravity
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.fragment.app.FragmentActivity
import com.compass.mobile.databinding.DialogCustmoerServiceBinding
import com.compass.mobile.framework.base.BaseDialog
import com.compass.mobile.framework.base.BaseDialogFragment
import com.compass.mobile.framework.constant.APPOINTMENT_ACTION
import com.compass.mobile.framework.constant.EVALUATION_ACTION
import com.compass.mobile.framework.constant.NURSE_ACTION
import com.compass.mobile.framework.constant.OK_ACTION
import com.compass.mobile.framework.constant.PRESCRIPTION_ACTION
import com.compass.mobile.framework.constant.TEST_ACTION
import com.compass.mobile.framework.constant.TEST_ORDER_ACTION
import com.compass.mobile.framework.constant.selfServicePhone
import com.compass.mobile.framework.constant.selfServiceWxCode
import com.compass.mobile.framework.ext.onClick
import com.compass.mobile.framework.utils.FocusViewUtils
import com.compass.mobile.framework.utils.QRCodeDealUtils
import com.compass.mobile.ui.activity.MyAppointmentActivity
import com.compass.mobile.ui.activity.MyInspectionActivity
import com.compass.mobile.ui.activity.NursingOrderActivity
import dp2px

/**
 * Description： 客服二维码
 * Date:2024/6/25 17:05
 */
class CustomerServiceCode {
    class Builder(activity: FragmentActivity, private var action: String) :
        BaseDialogFragment.Builder<Builder>(activity) {
        private val mBinding: DialogCustmoerServiceBinding =
            DialogCustmoerServiceBinding.inflate(LayoutInflater.from(activity))

        init {
            initView()
        }

        private fun initView() {
            setContentView(mBinding.root)
            setWidth(ViewGroup.LayoutParams.WRAP_CONTENT)
            setHeight(ViewGroup.LayoutParams.WRAP_CONTENT)
            setAnimStyle(BaseDialog.AnimStyle.DEFAULT)
            setGravity(Gravity.CENTER)
            setCancelable(true)
            FocusViewUtils.scaleView(mBinding.tvToOrder, 2)
            mBinding.tvToOrder.requestFocus()
            mBinding.tvToOrder.text = if (action == OK_ACTION) "确定" else "查看订单"
            mBinding.tvPhone.text = "客服电话：${UserInfoManager.getString(selfServicePhone)}"
            setTips()
            mBinding.imgWxCode.setImageBitmap(
                QRCodeDealUtils.generateQRCodeWithoutMarginBlack(
                    UserInfoManager.getString(selfServiceWxCode),
                    dp2px(400f),
                    dp2px(400f)
                )
            )
            mBinding.tvToOrder.onClick {
                dismiss()
                toOrder(action)
            }
        }

        private fun setTips() {
            when (action) {
                TEST_ORDER_ACTION, TEST_ACTION -> {
                    mBinding.refusalInfo.text =
                        "温馨提示：您已检验检查订单支付成功！点击下方按钮进入检验检查订单列表查看"
                }
                NURSE_ACTION->{
                    mBinding.refusalInfo.text =
                        "温馨提示：您已护理订单支付成功！点击下方按钮进入护理订单列表查看"
                }

                APPOINTMENT_ACTION -> {
                    mBinding.refusalInfo.text =
                        "温馨提示：您已预约成功！点击下方按钮可查看您的预约订单"
                }

                EVALUATION_ACTION -> {
                    mBinding.refusalInfo.text =
                        "温馨提示：评价成功！"
                }

                PRESCRIPTION_ACTION -> {
                    mBinding.refusalInfo.text =
                        "温馨提示：医生向您开具得处方已审核通过！"
                }

                else -> {
                    mBinding.refusalInfo.text =
                        "温馨提示：线上有任何疑问您可扫描下方二维码联系在线客服，也可直接拨打客服电话咨询。"
                }
            }

        }

        private fun toOrder(action: String) {
            when (action) {
                TEST_ORDER_ACTION, TEST_ACTION -> {
                    //检验检查订单
                    MyInspectionActivity.start(context)
                }

                NURSE_ACTION -> {
                    //护理订单
                    NursingOrderActivity.start(context)
                }

                APPOINTMENT_ACTION -> {
                    MyAppointmentActivity.start(context)
                }
            }
        }
    }
}